<?php
/**
 * 文章内容管理
 * @author cjli
 *
 */
class ArticleAction extends HomeAction
{
	public function _initialize()
	{
		parent::_initialize();
		$this->assign('robots','noindex,nofollow');
	}

	public function index()
	{
        send_http_status(404);
        $this->display(C('ERROR_PAGE'));
        exit;
		$articleCategoryList = CategoryModel::get_category_list_base(CategoryModel::TYPE_ARTICLE);
		if($articleCategoryList){
			foreach($articleCategoryList as &$cat){
				$cat['aritcleList'] = ArticleModel::get_article_list(array('catid' => $cat['id']));
			}
		}
//		dump($articleCategoryList);exit;
		$data['articleCategoryList'] = $articleCategoryList;
		$this->assign($data);
		$this->display('article_index');
	}

	public function articlelist()
	{
		$catid = intval($_GET['id']);
		if(!cookie('local_time')){
			redirect(url('/'));
		}
		$local_time =  strtotime(cookie('local_time'));//当地时间
		$beijing_time = time();//北京时间
		$time_difference = $local_time - time();
		if($time_difference < 0){
			$times['type'] = 0;
			$time_difference = (-1)*$time_difference;
		}else{
			$times['type'] = 1;
		}

		if(C('time_list')){
			$time_list =  explode(",",C('time_list'));
		}
		$this->assign('time_list',$time_list);
		$shiqu = round($time_difference/3600);
		$times['shiqu'] = $shiqu;

		$articleCategoryInfo = CategoryModel::get_category_info($catid, CategoryModel::TYPE_ARTICLE);
		if(!$articleCategoryInfo){
			redirect(url(''));
		}

		$where = array('catid' => $catid);

		//分页显示数
		$limit_number = C('product_list_num');

		$article_count = ArticleModel::get_article_count($where);
		$articleList = array();
		if($article_count){
			import("ORG.Util.Page");// 导入分页类
			$Page = new Page($productCount, $limit_number);// 实例化分页类 传入总记录数和每页显示的记录数
			$pageShow = $Page->show();// 分页显示输出
			$data['pageShow'] = $pageShow;
			$articleList = ArticleModel::get_article_list($where, $Page->firstRow, $Page->listRows);
		}


		$data['categoryInfo'] = $articleCategoryInfo;
		$data['articleList'] = $articleList;
		$data['article_count'] = $article_count;
		$data['docTitle'] = $articleCategoryInfo['meta_title'];
		$data['docKeywords'] = $articleCategoryInfo['meta_keywords'];
		$data['docDescription'] = $articleCategoryInfo['meta_description'];
		$this->assign($data);
		$this->assign('local_time',$local_time);
		$this->assign('times',$times);
		$this->display('articlelist');
	}

	public function info()
	{
		if(!cookie('local_time')){
			redirect(url('/'));
		}
		$local_time =  strtotime(cookie('local_time'));//当地时间
		$beijing_time = time();//北京时间
		$time_difference = $local_time - time();
		if($time_difference < 0){
			$times['type'] = 0;
			$time_difference = (-1)*$time_difference;
		}else{
			$times['type'] = 1;
		}

		if(C('time_list')){
			$time_list =  explode(",",C('time_list'));
		}
		$this->assign('time_list',$time_list);
		$shiqu = round($time_difference/3600);
		$times['shiqu'] = $shiqu;

		$aid = intval($_GET['id']);
		$articleInfo = ArticleModel::get_article_info($aid);
		if(!$articleInfo){
			redirect(url(''));
		}

		//文章分类
		$articleCategoryInfo = CategoryModel::get_category_info($articleInfo['catid'], CategoryModel::TYPE_ARTICLE);
		//分类列表
		//$articleCategoryList = CategoryModel::get_category_list_base(CategoryModel::TYPE_ARTICLE);

		$data['categoryInfo'] = $articleCategoryInfo;
		$data['articleInfo'] = $articleInfo;
		$data['docTitle'] = $articleInfo['meta_title'];
		$data['docKeywords'] = $articleInfo['meta_keywords'];
		$data['docDescription'] = $articleInfo['meta_description'];
		$this->assign($data);
		$this->assign('local_time',$local_time);
		$this->assign('times',$times);
		$this->display('articleinfo');
	}

	public function help()
	{
		$id = intval($_GET['id']);
		if(!$id){
			redirect(url(''));
		}
		$pageInfo = ArticleModel::get_page_info($id);
		$data = array(
    		'docTitle' => $pageInfo['meta_title'] ? $pageInfo['meta_title'] : $pageInfo['name'],
    		'docKeywords' => $pageInfo['meta_keywords'],
    		'docDescription' => $pageInfo['meta_description'],
    	);
		if(!$pageInfo){
			redirect(url(''));
		}
		$this->assign('pageInfo', $pageInfo);
		$this->assign($data);
		$this->display('help');
	}

	public function appointments()
	{
		global $user, $language_id;
		$data = array(
    		'docTitle' => L('meta_title_appointments'),
    		'docKeywords' => L('meta_keywords'),
    		'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		if(!cookie('local_time')){
			redirect(url('/'));
		}
		$local_time =  strtotime(cookie('local_time'));//当地时间
		$beijing_time = time();//北京时间
		$time_difference = $local_time - time();
		if($time_difference < 0){
			$times['type'] = 0;
			$time_difference = (-1)*$time_difference;
		}else{
			$times['type'] = 1;
		}

		if(C('time_list')){
			$time_list =  explode(",",C('time_list'));
		}
		$this->assign('time_list',$time_list);
		$shiqu = round($time_difference/3600);
		$times['shiqu'] = $shiqu;
		if($_POST['dosubmit']){

			if(!UserModel::logged()){
				$this->redirect(url('user/login'), array('src' => base64_encode(url('article/appointments'))));
			}

			$post = $_POST;
			//$yuyue_time2 = strtotime(''.$post['yuyue_time2'].'');
			if(empty($post['yuyue_time'])) $post['yuyue_time'] = date('Y-m-d H',time());

			$order_phone_db = D('order_phone');
			$data = array(
				'language_id' => $language_id,
				'phone' => $post['phone'],
				'content' => $post['content'],
				'time' => time(),
				'yuyue_time' => (strtotime($post['yuyue_time'].":0:0") + $times['shiqu']*3600 ),
				'user_id' => isset($user) && $user ? $user['id'] : 0,
				'username' => isset($user) && $user ? $user['first_name']. ' ' . $user['first_name'] : null,
				'ip' => get_client_ip(),
				'yuyue_time2' => strtotime($post['yuyue_time'].":0:0"),
			);
			$lastId = $order_phone_db->add($data);

			if($lastId){
				$this->success(L('_OPERATION_SUCCESS_'), url('article/appointments'));exit;
			}else{
				$this->error(L('_OPERATION_FAIL_'), url('article/appointments'));
			}
		}
		$this->assign('local_time',$local_time);
		$this->assign('times',$times);
		$this->display('appointments');
	}
	public function customize()
	{
		$data = array(
    		'docTitle' => L('meta_title_customize'),
    		'docKeywords' => L('meta_keywords'),
    		//'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->display('why-customize');
	}
	public function how_customize()
	{
		$data = array(
    		'docTitle' => L('meta_title_how_customize'),
    		'docKeywords' => L('meta_keywords'),
    		//'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->display('how-customize');
	}

	public function quality()
	{
		$data = array(
    		'docTitle' => L('meta_title_quality'),
    		'docKeywords' => L('meta_keywords'),
    		//'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->display('quality');
	}

	public function delivery()
	{
		$data = array(
    		'docTitle' => L('meta_title_delivery'),
    		'docKeywords' => L('meta_keywords'),
    		//'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->display('delivery');
	}

	public function fitting()
	{
		$data = array(
    		'docTitle' => L('meta_title_fitting'),
    		'docKeywords' => L('meta_keywords'),
    		//'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->display('fitting');
	}

	public function materials()
	{
		$data = array(
    		'docTitle' => L('meta_title_materials'),
    		'docKeywords' => L('meta_keywords'),
    		//'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->assign('robots','nofollow');
		$this->display('materials');
	}

	public function about_us()
	{
		$data = array(
    		'docTitle' => L('meta_title_about_us'),
    		'docKeywords' => L('meta_keywords'),
    		//'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->display('about-us');
	}

	/*shopping guide*/
	public function shopping_guide()
	{
		$this->display('shopping_guide');
	}

	public function join_us()
	{
		$data = array(
    		'docTitle' => L('meta_title_join_us'),
    		'docKeywords' => L('meta_keywords'),
    		'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->display('join-us');
	}
	public function reviews()
	{
		$data = array(
    		'docTitle' => L('meta_title_reviews'),
    		'docKeywords' => L('meta_keywords'),
    		'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->display('review');
	}
	public function design(){
		$this->display('design');
	}
	public function garment_sample(){
		$this->display('garment-sample');
	}
	public function tailor(){
		$data = array(
    		'docTitle' => L('meta_title_tailor'),
    		'docKeywords' => L('meta_keywords'),
    		//'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->display('tailor');
	}
	public function sew(){
		$data = array(
    		'docTitle' => L('meta_title_sew'),
    		'docKeywords' => L('meta_keywords'),
    		//'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->display('sew');
	}
	public function iron(){
		$data = array(
    		'docTitle' => L('meta_title_iron'),
    		'docKeywords' => L('meta_keywords'),
    		//'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->display('iron');
	}
	public function quality_testing(){
		$this->display('quality-testing');
	}
	public function questionary(){
        send_http_status(301);
        redirect(url(''));
        exit;
		$data = array(
    		'docTitle' => L('meta_title_questionary'),
    		'docKeywords' => L('meta_keywords'),
    		//'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->display('questionary/index');
	}
	public function ajax_questionary(){
		global $user, $language_id;
		$post =$_POST;

		$result = array(
			'status' => 'failure',
			'message' =>'',
		);
		if(UserModel::logged()){
			$survey_db = D('user_bonus');
			$survey_info = $survey_db->where(array('user_id'=>$user['id'],'bonus_id'=>4))->find();
			if($survey_info){
					$result['status'] = 'failure';
					$result['message'] = L('_QUESTIONNAIRE_');
					echo json_encode($result);exit;
			}else{
				$questionnaire = ArticleModel::Surveyinsert($post);

				if($questionnaire){
				session('survey',$questionnaire);
				UserModel::user_bonus('survey',$user['id']);
				}
				$result['status'] = 'success';
				$result['message'] =L('Red_Prompt');
				echo json_encode($result);exit;
			}
		}else{
			$questionnaire = ArticleModel::Surveyinsert($post);
				if($questionnaire){
				session('survey',$questionnaire);
				}
			}
				$result['status'] = 'success';
				$result['message'] =L('Red_envelope');
				echo json_encode($result);exit;
	}
	public function coupon(){
		$data = array(
    		'docTitle' => L('meta_title_coupon'),
    		'docKeywords' => L('meta_keywords'),
    		//'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		//设置自动输入优惠码
		if(isset($_GET['code']) && $_GET['code'] == '3X2SHIRTS'){
            cookie('self_coupon', $_GET['code'],'expire=604800&path=/&domain='.APP_DOMAIN);
            redirect('/category/2062-shirts');
		}
		$this->display('coupon');
	}
	
	//获取当地时间
	public function local_time(){
		$local_time = trim($_REQUEST['local_time']);
		if($local_time){
			cookie('local_time', $local_time);
		}

	}
	
    //媒体新闻
	public function media_reports($media_type = 3){
		$data = array(
    		'docTitle' => L('meta_title_media_reports'),
    		'docKeywords' => L('meta_keywords'),
    		//'docDescription' => L('meta_description'),
    	);
    	$this->assign('robots','');
		$this->assign($data);
	    $this->assign('canonical' ,url('/article/media_reports'));

		$pageRows = 6;
		$media_db = D('media');
		$media_goods_db=D('product');
		$count = $media_db->where(array('media_type'=>$media_type))->count();
		import("ORG.Util.PageListSimple");
		$Page = new Page($count,$pageRows);
		$pageShow = $Page->show();
		$media_db->limit($Page->firstRow.','.$Page->listRows);
		$mediaInfo = $media_db->where(array('media_type'=>$media_type))->order('listorder asc,id desc')->select();
		
		//如果是顾客评论
		$media_goods_info=array();
		if($media_type==3){
		    //查找相关产品
		    foreach ($mediaInfo  as $v){
		        if($v['product_id']){
        		      $goods = $media_goods_db->query("select a.*,b.name from product a  left join product_items b on b.product_id=a.id where b.language_id=6 and a.is_delete=0 and  a.display=1 and a.id in(".$v['product_id'].")");
        		      foreach ($goods as $k){
        		          $media_goods_info[$k['id']]=$k;
        		         // array_push($media_goods_info);
        		      }
		        }
		    }
		}
		//print_r($media_goods_info);exit;
		$this->assign('media_goods_info',$media_goods_info);
		$this->assign('media_type',$media_type);
		$this->assign('pageShow', $pageShow);
		$this->assign('mediaInfo',$mediaInfo);
		$this->display('media_reports');
	}
	 //媒体新闻
	public function ajax_media_reports(){
		$media_type = $_GET['media_type'];
		$data = array(
    		'docTitle' => L('meta_title_media_reports'),
    		'docKeywords' => L('meta_keywords'),
    		'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$pageRows = 6;
		$media_db = D('media');
		$media_goods_db=D('product');
		$count = $media_db->where(array('media_type'=>$media_type))->count();
		import("ORG.Util.PageListSimple");
		$Page = new Page($count,$pageRows);
		$pageShow = $Page->show();
		$mediaInfo = $media_db->where(array('media_type'=>$media_type))->order('listorder asc,id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		//如果是顾客评论
		$media_goods_info=array();
		if($media_type==3){
		    //查找相关产品
		    foreach ($mediaInfo  as $v){
		        if($v['product_id']){
		            $goods = $media_goods_db->query("select a.*,b.name from product a  left join product_items b on a.id=b.product_id where b.language_id=6 and a.is_delete=0 and  a.display=1 and a.id in(".$v['product_id'].")");
		            foreach ($goods as $k){
		                $media_goods_info[$k['id']]=$k;
		                // array_push($media_goods_info);
		            }
		        }
		    }
		}
		//print_r($media_goods_info);exit;
		//$this->assign('media_goods_info',$media_goods_info);
		$this->assign('media_type',$media_type);
		$this->assign('pageShow', $pageShow);
		$this->assign('mediaInfo',$mediaInfo);
		echo json_encode(array(
			'data'=>$this->fetch('media_reports_list'),
			'page'=>$pageShow,
		    'media_goods_info'=>$this->get_media_goods_info($media_goods_info)
		));
	}
	private function get_media_goods_info($media_goods_info){
	    $str="";
	    
	    foreach($media_goods_info as $v){
	        $goods_img=$v['goods_img']?$v['goods_img']:"/statics/front/images/icons/loading.gif";
	        $ml_img=$v['ml_img']?$v['ml_img']:"/statics/front/images/icons/loading.gif";
	    $str.='
	        
	  <li>
      <a href="'.url('').'product/'.$v['id'].'-'.$v['name'].'">
        <img data-original="'.$goods_img.'" alt="'.$v['name'].'"  src="'.$goods_img.'">
        <img data-original="'.$ml_img.'" alt="'.$v['name'].'" class="hide"  src="'.$ml_img.'">
      </a>
      <strong><a href="'.url('').'product/'.$v['id'].'-'.$v['name'].'">'.$v['name'].'</a></strong>
      <p>￥'.$v['advanced_price'].'</p>
      </li>
	        
	        
	        ';
	    }
	  return $str;
	}
	//terms and conditions页面
	public function terms_conditions(){
        send_http_status(404);
        $this->display(C('ERROR_PAGE'));
        exit;
		$data = array(
    		'docTitle' => L('meta_title_terms_conditions'),
    		'docKeywords' => L('meta_keywords'),
    		'docDescription' => L('meta_description'),
    	);
		$this->assign($data);
		$this->display('terms_and_conditions');
	}
	public function returnExchangePolicy() {
		$this->assign($data);
		$this->display('return_exchange_policy');
	}
}